﻿
// MFCCef.cpp: 定义应用程序的类行为。
//

#include "pch.h"
#include "framework.h"
#include "MFCCef.h"
#include "MFCCefDlg.h"

#include "include/cef_client.h"
#include "include/cef_app.h"
#include "CCefBrowserApp.h"
#include "obj.h"
#include "log.h"
// CMFCCefApp

BEGIN_MESSAGE_MAP(CMFCCefApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CMFCCefApp 构造

CMFCCefApp::CMFCCefApp()
{
	// TODO: 在此处添加构造代码，
	// 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的 CMFCCefApp 对象

CMFCCefApp theApp;
// 全局的dm对象
dmsoft* g_dm = NULL;

// CMFCCefApp 初始化

BOOL CMFCCefApp::InitInstance()
{
	// 初始化COM模型为mta
	CoInitializeEx(NULL, 0);
	// 初始化日志
	LogInit();
	g_dm = new dmsoft;
	if (g_dm == NULL)
	{
		Log(_T("对象创建失败"));
		return 0;
	}
	// 收费注册
	
	long dm_ret = g_dm->Reg(_T("xf30557fc317f617eead33dfc8de3bdd4ab9043"), _T("xflisw61jzmb700"));
	//long dm_ret = g_dm->RegEx(_T("xf30557fc317f617eead33dfc8de3bdd4ab9043"), _T("xflisw61jzmb700"), L"121.204.252.143");
	if (dm_ret != 1)
	{
		Log(_T("收费注册失败,返回值:%d"), dm_ret);
		delete g_dm;
		return 0;
	}

	Log(g_dm->Ver());
	/*
	*/
	CWinApp::InitInstance();

	//## cef ##

	void* sandbox_info = NULL;
	CefMainArgs main_args(m_hInstance);
	CefRefPtr<CCefBrowserApp> app(new CCefBrowserApp);

	int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info);
	if (exit_code >= 0)
	{
		return exit_code;
	}
	CefSettings settings;
	CefSettingsTraits::init(&settings);
	settings.no_sandbox = true;
	settings.multi_threaded_message_loop = true;
	settings.ignore_certificate_errors = true;
	settings.command_line_args_disabled = true;
	CefInitialize(main_args, settings, app.get(), sandbox_info);

	//## cef ##

	//////////////////////////////////////////////////////////////////////////////////////////////
	 // Enable High-DPI support on Windows 7 or newer.
	/*
	CefEnableHighDPISupport();

	void* sandbox_info = nullptr;

#if defined(CEF_USE_SANDBOX)
	// Manage the life span of the sandbox information object. This is necessary
	// for sandbox support on Windows. See cef_sandbox_win.h for complete details.
	CefScopedSandboxInfo scoped_sandbox;
	sandbox_info = scoped_sandbox.sandbox_info();
#endif

	// Provide CEF with command-line arguments.
	CefMainArgs main_args(m_hInstance);

	// SimpleApp implements application-level callbacks for the browser process.
	// It will create the first browser instance in OnContextInitialized() after
	// CEF has initialized.

	CefRefPtr<CCefBrowserApp> app(new CCefBrowserApp);

	// CEF applications have multiple sub-processes (render, GPU, etc) that share
	// the same executable. This function checks the command-line and, if this is
	// a sub-process, executes the appropriate logic.
	int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info);
	if (exit_code >= 0) {
		// The sub-process has completed so return here.
		return exit_code;
	}

	// Parse command-line arguments for use in this method.
	CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
	command_line->InitFromString(::GetCommandLineW());

	// Specify CEF global settings here.
	CefSettings settings;

	if (command_line->HasSwitch("enable-chrome-runtime")) {
		// Enable experimental Chrome runtime. See issue #2969 for details.
		settings.chrome_runtime = true;
	}

#if !defined(CEF_USE_SANDBOX)
	settings.no_sandbox = true;
#endif



	// Initialize CEF.
	CefInitialize(main_args, settings, app.get(), sandbox_info);
	//CefRefPtr<CefCookieManager> cm = CefCookieManager::GetGlobalManager(nullptr);

	//::SetWindowPos()
	// Run the CEF message loop. This will block until CefQuitMessageLoop() is
	// called.
	//CefRunMessageLoop();

	// Shut down CEF.
	//CefShutdown();
	*/
	//////////////////////////////////////////////////////////////////////////////////////////////
	// 创建 shell 管理器，以防对话框包含
	// 任何 shell 树视图控件或 shell 列表视图控件。
	CShellManager *pShellManager = new CShellManager;

	// 激活“Windows Native”视觉管理器，以便在 MFC 控件中启用主题
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小，则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串，
	// 例如修改为公司或组织名
	//SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
	
	CMFCCefDlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: 在此放置处理何时用
		//  “确定”来关闭对话框的代码
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: 在此放置处理何时用
		//  “取消”来关闭对话框的代码
	}
	else if (nResponse == -1)
	{
		TRACE(traceAppMsg, 0, "警告: 对话框创建失败，应用程序将意外终止。\n");
		TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件，则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
	}

	// 删除上面创建的 shell 管理器。
	if (pShellManager != nullptr)
	{
		delete pShellManager;
	}

#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
	ControlBarCleanUp();
#endif

	// 由于对话框已关闭，所以将返回 FALSE 以便退出应用程序，
	//  而不是启动应用程序的消息泵。
	
	//关闭CEF
	
	CefQuitMessageLoop();
	CefShutdown();

	return FALSE;
}

